package com.example.demo.service.Impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.example.demo.ao.CreateMeetingAo;
import com.example.demo.ao.InsertMeetingMinutesAo;
import com.example.demo.ao.MeetingMinutesAo;
import com.example.demo.ao.SysUser;
import com.example.demo.mapper.MeetingMinutesMapper;
import com.example.demo.service.MeetingMinutesService;
import com.example.demo.service.UserService;
import com.example.demo.vo.MeetingMinutesVo;
import com.example.demo.vo.MeetingRoomVo;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class MeetingMinutesServiceImpl implements MeetingMinutesService {


    @Autowired
    private MeetingMinutesMapper meetingMinutesMapper;

    @Autowired
    private UserService userService;


    @Override
    public Page<MeetingMinutesVo> getMeetingMinutes(MeetingMinutesAo meetingMinutesAo) {
        int pageNo = 0;
        if (meetingMinutesAo.getPageNo() != null) {
            pageNo = (meetingMinutesAo.getPageNo() - 1) * meetingMinutesAo.getPageSize();
        } else {
            pageNo = (meetingMinutesAo.getEndPageNo() * meetingMinutesAo.getPageSize()) - ((meetingMinutesAo.getStartPageNo() - 1) * meetingMinutesAo.getPageSize());
        }
        Page<MeetingMinutesVo> page = new Page<>();
        List<MeetingMinutesVo> meetingMinutes = meetingMinutesMapper.getMeetingMinutes(meetingMinutesAo, pageNo, meetingMinutesAo.getPageSize());
        page.setRecords(meetingMinutes);
        page.setTotal(meetingMinutesMapper.getMeetingMinutesTotal(meetingMinutesAo));

        return page;
    }

    @Override
    public List<MeetingMinutesVo> getMeetingMinutesInfo(MeetingMinutesAo meetingMinutesAo) {
        return meetingMinutesMapper.getMeetingMinutesInfo(meetingMinutesAo);
    }


    @Override
    public String insertMeetingMinutes(InsertMeetingMinutesAo insertMeetingMinutesAo, HttpServletRequest request) {
        int result = 0;
        SysUser loginUser = userService.getLoginUser(request);
        insertMeetingMinutesAo.setCreateBy(loginUser.getUserId());
        insertMeetingMinutesAo.setCreateTime(new Date());
        System.out.println(insertMeetingMinutesAo);
        result = meetingMinutesMapper.insertMeetingMinutes(insertMeetingMinutesAo);
        return result > 0 ? "success" : "error";
    }

    @Override
    public String updateMeetingMinutes(InsertMeetingMinutesAo insertMeetingMinutesAo, HttpServletRequest request) {
        int result = 0;
        SysUser loginUser = userService.getLoginUser(request);
        insertMeetingMinutesAo.setUpdateBy(loginUser.getUserId());
        insertMeetingMinutesAo.setUpdateTime(new Date());
        if ("01".equals(insertMeetingMinutesAo.getType())) {
            result = meetingMinutesMapper.updateMeetingMinutes(insertMeetingMinutesAo);
        } else if ("02".equals(insertMeetingMinutesAo.getType())) {
            insertMeetingMinutesAo.setIsDeleted(1);
            result = meetingMinutesMapper.updateMeetingMinutes(insertMeetingMinutesAo);
        }

        return result > 0 ? "success" : "error";
    }


    //会议室
    @Override
    public List<MeetingRoomVo> getMeetingRoom() {
        return meetingMinutesMapper.getMeetingRoom();
    }


    @Override
    public String insertMeetingRoom(CreateMeetingAo createMeetingAo, HttpServletRequest request) {
        int result = 0;
        List<MeetingRoomVo> meetingRoom = meetingMinutesMapper.getMeetingRoom();
        for (MeetingRoomVo meetingRoomVo : meetingRoom) {
            if (createMeetingAo.getRoomNo().equals(meetingRoomVo.getRoomNo())) {
                return "会议室已存在";
            }
        }
        SysUser loginUser = userService.getLoginUser(request);
        createMeetingAo.setCreateName(loginUser.getUserName());
        createMeetingAo.setCreateBy(String.valueOf(loginUser.getUserId()));
        createMeetingAo.setCreateTime(new Date());
        createMeetingAo.setRoomStatus("00");
        result = meetingMinutesMapper.insertMeetingRoom(createMeetingAo);
        return result > 0 ? "success" : "error";
    }


    @Override
    public List<MeetingRoomVo> getMeetingRoomInfo(CreateMeetingAo createMeetingAo) {
        List<MeetingRoomVo> meetingRoomInfo = meetingMinutesMapper.getMeetingRoomInfo(createMeetingAo);
        Date currenDate = new Date();
        for (MeetingRoomVo meetingRoomVo : meetingRoomInfo) {
            if (currenDate.after(meetingRoomVo.getStartTime()) && currenDate.before(meetingRoomVo.getEndTime()) && meetingRoomVo.getRoomStatus().equals("01")) {
                createMeetingAo.setRoomStatus("02");
                createMeetingAo.setId(meetingRoomVo.getId());
                int cnt = meetingMinutesMapper.updateMeetingRoomApplication(createMeetingAo);
                System.out.println("更新了" + cnt + "条");
            } else if (currenDate.after(meetingRoomVo.getEndTime()) && meetingRoomVo.getRoomStatus().equals("02")) {
                createMeetingAo.setRoomStatus("03");
                createMeetingAo.setId(meetingRoomVo.getId());
                int cnt = meetingMinutesMapper.updateMeetingRoomApplication(createMeetingAo);
                System.out.println("更新了使用完成状态" + cnt + "条");
            }
        }
        return meetingRoomInfo;
    }


    @Override
    public String insertMeetingApplication(CreateMeetingAo createMeetingAo, HttpServletRequest request) {
        int result = 0;
        SysUser loginUser = userService.getLoginUser(request);
        createMeetingAo.setCreateName(loginUser.getUserName());
        createMeetingAo.setCreateBy(String.valueOf(loginUser.getUserId()));
        createMeetingAo.setCreateTime(new Date());
        createMeetingAo.setApplicantId(loginUser.getUserId());
        createMeetingAo.setApplicantName(loginUser.getUserName());
        createMeetingAo.setApplyTime(new Date());
        result = meetingMinutesMapper.insertMeetingApplication(createMeetingAo);
        return result > 0 ? "success" : "error";
    }


    @Override
    public Page<MeetingRoomVo> getUserRoomInfo(CreateMeetingAo createMeetingAo, HttpServletRequest request) {
        int pageNo = 0;
        if (createMeetingAo.getPageNo() != null) {
            pageNo = (createMeetingAo.getPageNo() - 1) * createMeetingAo.getPageSize();
        } else {
            pageNo = (createMeetingAo.getEndPageNo() * createMeetingAo.getPageSize()) - ((createMeetingAo.getStartPageNo() - 1) * createMeetingAo.getPageSize());
        }
        SysUser loginUser = userService.getLoginUser(request);
        createMeetingAo.setApplicantId(loginUser.getUserId());
        List<MeetingRoomVo> userRoomInfo = meetingMinutesMapper.getUserRoomInfo(createMeetingAo, pageNo, createMeetingAo.getPageSize());
        for (MeetingRoomVo meetingRoomVo : userRoomInfo) {
            if ("01".equals(createMeetingAo.getButtonType())) {
                createMeetingAo.setRoomStatus("04");
                createMeetingAo.setUpdateBy(loginUser.getUserId());
                int cnt = meetingMinutesMapper.updateMeetingRoomApplication(createMeetingAo);
                System.out.println("取消了"+cnt+"条,取消的id是"+meetingRoomVo.getId());
            }
        }
        Page<MeetingRoomVo> page = new Page<>();
        page.setRecords(userRoomInfo);
        page.setTotal(meetingMinutesMapper.getUserRoomCount(createMeetingAo));
        return page;
    }



    @Override
    public Page<MeetingRoomVo> getRoomAnalyse(CreateMeetingAo createMeetingAo) {
        int pageNo = 0;
        if (createMeetingAo.getPageNo() != null) {
            pageNo = (createMeetingAo.getPageNo() - 1) * createMeetingAo.getPageSize();
        } else {
            pageNo = (createMeetingAo.getEndPageNo() * createMeetingAo.getPageSize()) - ((createMeetingAo.getStartPageNo() - 1) * createMeetingAo.getPageSize());
        }
        List<MeetingRoomVo> roomAnalyse = meetingMinutesMapper.getRoomAnalyse(createMeetingAo, pageNo, createMeetingAo.getPageSize());
        Page<MeetingRoomVo> page = new Page<>();
        page.setRecords(roomAnalyse);
        page.setTotal(meetingMinutesMapper.getRoomAnalyseCount(createMeetingAo));
        return page;
    }


}
